//  Arduino code to talk to a APSX RFID Module
//  Ported from Python code
//  Not a complete set of functions but...
//
//  17 March 2009   TCM       - Created
//

int PresetValue = 0xffff;//for CRC calculation
int Polynomial = 0x8408;//for CRC calculation
int val = 0, sleep=100;
byte pass_read_single[13] = {0x0D,0x00,0x03,0x04,0x45,0x03,0x20,0x01,0x12,0x1B,0x65,0x9A};
byte reset_to_ready[6] = {0x05,0x03,0x03,0x26,0x1B,0x61};
byte GetUID[7] = {0x06,0x0C,0x27,0x01,0x00,0x2A,0x50};
byte tagAquiet[14]={0x0D,0x00,0x23,0x02,0xEE,0xE9,0x8A,0x12,0x00,0x01,0x04,0xE0,0xC7,0x3F};
//byte tagAquiet[20] = {0x01,0x14,0x00,0x03,0x04,0x45,0x23,0x02,0xee,0xe9,0x8a,0x12,0x00,0x01,0x04,0xe0,0xE1,0x7C,0xE7,0x0F};
byte tagBquiet[14]={0x0D,0x00,0x23,0x02,0xF1,0x6F,0x8A,0x12,0x00,0x01,0x04,0xE0,0xDD,0x0B};
byte tagCquiet[14]={0x0D,0x00,0x23,0x02,0x85,0xF1,0x8A,0x12,0x00,0x01,0x04,0xE0,0x8F,0x20};
byte tagDquiet[14]={0x0D,0x00,0x23,0x02,0x8D,0xEF,0x8A,0x12,0x00,0x01,0x04,0xE0,0xDB,0x3E};
byte tagEquiet[14]={0x0D,0x00,0x23,0x02,0xC3,0xFE,0x8A,0x12,0x00,0x01,0x04,0xE0,0x3B,0x41};
byte tagFquiet[14]={0x0D,0x00,0x23,0x02,0xFE,0xB4,0x8A,0x12,0x00,0x01,0x04,0xE0,0x3F,0x38};
byte tagGquiet[14]={0x0D,0x00,0x23,0x02,0x48,0xB2,0x8A,0x12,0x00,0x01,0x04,0xE0,0x29,0x5B};
byte tagHquiet[14]={0x0D,0x00,0x23,0x02,0x09,0xD8,0x8a,0x12,0x00,0x01,0x04,0xE0,0xB5,0xF2};
//Tag Identifiers. The last two bytes (CRC) change when in a command
//A) 0000eee98a12000104e0e17c
//B) 0000f16f8a12000104e0fb48
//C) 000085f18a12000104e0a963
//D) 00008def8a12000104e0fd7d
//E) 0000c3fe8a12000104e01d02
//F) 0000feb48a12000104e0197b
//G) 000048b28a12000104e00f18
//H) 000009d88a12000104e093b1

#define SIZEOFARRAY(x)  (sizeof (x) / sizeof (x[0]));

//Function to tell a tag to be quiet
byte Stay_Quiet(int tag)
{
//  "Tell a tag to shut up
  Serial.write(0xF7);
  delay(sleep);
  Serial.write(0xF8);
  delay(sleep);
  if (tag == 1){
    //Tag A: eee98a12000104e0 \ e17c
//    Serial.println("tag A quiet");
    Serial.write(tagAquiet,14);
  }
  else if(tag == 2){
    //Tag B: f16f8a12000104e0 \ fb48
//    Serial.println("tag B quiet");
    Serial.write(tagBquiet,14);
  }
  else if(tag == 3){
    //Tag C: 85f18a12000104e0 \ a963
//    Serial.println("tag C quiet");
    Serial.write(tagCquiet,14);
  }
  else if(tag == 4){
    //Tag D: 8def8a12000104e0 \ fd7d
//    Serial.println("tag D quiet");
    Serial.write(tagDquiet,14);
  }
  else if(tag == 5){
    //Tag E: c3fe8a12000104e0 \ 1d02
//    Serial.println("tag E quiet");
    Serial.write(tagEquiet,14);
  }
  else if(tag == 6){
    //Tag F: feb48a12000104e0 \ 197b
//    Serial.println("tag F quiet");
    Serial.write(tagFquiet,14);
  }
  else if(tag == 7){
    //Tag G: 48b28a12000104e0 \ 0f18
//    Serial.println("tag G quiet");
    Serial.write(tagGquiet,14);
  }
  else if(tag == 8){
    //Tag H: 09d88a12000104e0 \ 93b1
//    Serial.println("tag H quiet");
    Serial.write(tagHquiet,14);
  }
  else{
    return 0;
  }
  delay(sleep);
}


void red_LED(boolean on)
{
    if (on=true){
      Serial.print(0xFD);
    }
    else{
        Serial.write(0xFC);
    }
    delay(sleep);
}

void green_LED(boolean on)
{
    if (on=true) {
        Serial.print(0xFF);
    }
    else {
        Serial.write(0xFE);
    }
    delay(sleep);
}

void setup()
{
   int ii=0;
   // Start serial port 19200 bps
   Serial.begin(19200);
   Serial.flush();
   red_LED(true);
   delay(sleep);
   red_LED(false);
   delay(sleep);
   green_LED(true);
   delay(sleep);
   Serial.write(0xF7);
   delay(sleep);
   Serial.write(0xF8);
   delay(sleep);
   Serial.write(0xFB);
   delay(sleep);
   Serial.flush();
   Serial.println("RFID module started.");
}

//Function to read a tag
void read_UID(byte *s){
    //Read a UID or 0 if not exactly 12 bytes
    int ii=0;
    Serial.write(0xFA);
    for(ii=0; ii<12; ii++){
      s[ii] = Serial.read();
      Serial.print(s[ii],HEX);
    }
}

//Continuous loop for doing the RFID reading
void continuous_monitor() {
  int count = 0;
  byte b[12];
  while (count < 10){
    // get a button
    read_UID(b);
    delay(sleep);
    count ++;
    if (b != 0){
      green_LED(true);
      Serial.print("got a button");
      while (b){
        delay(sleep);
        read_UID(b);
      }
      green_LED(false);
    }
    else{
      red_LED(true);
      delay(sleep);
      red_LED(false);
      delay(sleep);
    }
  }
}

void loop(){
  int xx=1, yy=1, ii=0, sum=0;
  byte UID[12];
  for (xx=1; xx<9; xx++) {
    ii=0;
    Serial.write(0xF7);
    delay(sleep);
    Serial.write(0xF8);
    delay(sleep);
    Serial.write(reset_to_ready,6);
    delay(sleep);
    //Serial.read();
    for (yy=1; yy<9; yy++) {
      if(xx != yy) {
        Stay_Quiet(yy);
        //Serial.flush();
      }
    }
    Serial.flush();
//    Serial.write(GetUID,7);
//    Serial.write(0xFA);
//    read_UID(UID);
//    while(Serial.available()>0){
//      UID[ii] = Serial.read();
//    }
//    val = SIZEOFARRAY(UID);
//    Serial.println(val);
//    for (ii=0; ii<val; ii++) {
//      Serial.print(UID[ii],HEX);
//    }
//    Serial.println(" End of tag.");
    Serial.write(0xFA);
    while(Serial.available()>0){
      UID[ii] = Serial.read();
      sum += UID[ii];
      ii++;
    }
    val = SIZEOFARRAY(UID);
    if (val==12 & sum>0){
      green_LED(true);
      Serial.println("got a button");
      for (ii=0; ii<val; ii++) {
        Serial.print(UID[ii],HEX);
      }
      green_LED(false);
      sum = 0;
      Serial.flush();
    }
    else{
      red_LED(true);
      delay(sleep);
      red_LED(false);
      delay(sleep);
    }
    Serial.flush();
  }
}
